package k8s

import (
	"context"

	corev1 "k8s.io/api/core/v1"
	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes"
	core "k8s.io/client-go/kubernetes/typed/core/v1"
)

func NewService(c *kubernetes.Clientset) *Service {
	return &Service{
		core: c.CoreV1(),
	}
}

type Service struct {
	core core.CoreV1Interface
}

// 获取Service列表
func (s *Service) GetServices(ctx context.Context, namespace string, req *v1.ListOptions) (*corev1.ServiceList, error) {
	return s.core.Services(namespace).List(ctx, *req)
}

// 获取Service详情
func (s *Service) GetServiceDetails(ctx context.Context, namespace, name string) (*corev1.Service, error) {
	return s.core.Services(namespace).Get(ctx, name, v1.GetOptions{})
}

// 创建Service
func (s *Service) CreateService(ctx context.Context, namespace string, service *corev1.Service) (*corev1.Service, error) {
	return s.core.Services(namespace).Create(ctx, service, v1.CreateOptions{})
}

// 更新Service
func (s *Service) UpdateService(ctx context.Context, namespace string, service *corev1.Service) (*corev1.Service, error) {
	return s.core.Services(namespace).Update(ctx, service, v1.UpdateOptions{})
}

// 删除Service
func (s *Service) DeleteService(ctx context.Context, namespace, name string) error {
	return s.core.Services(namespace).Delete(ctx, name, v1.DeleteOptions{})
}
